import numpy as np
import zlib


class MapTool:
    # DefineMap → MapConvert | np.int8 → 二值化 → np.uint8
    @staticmethod
    def MapConvert(map_data, dsize):  # 将地图数据转化为图片
        map_data_copy = map_data.copy()
        for i in range(len(map_data_copy)):
            if map_data_copy[i] == -1:  # 未知区域 灰
                map_data_copy[i] = 128
            elif map_data_copy[i] == 0:  # 空白区域 白
                map_data_copy[i] = 255
            elif map_data_copy[i] == 100:
                map_data_copy[i] = 0  # 障碍物 黑
        map_img = np.reshape(map_data_copy, dsize).astype(np.uint8)
        return map_img

    @staticmethod
    def MapEncode(org_data):  # 编码地图(np.int8)
        return zlib.compress(org_data, level=6)

    @staticmethod
    def MapDecode(ziped_data):  # 解码地图(np.int8)
        return zlib.decompress(ziped_data)

    @staticmethod
    def DefineMap(dsize):  # 预创建指定大小空地图(np.int8)
        return np.full(dsize[0] * dsize[1], -1, dtype=np.int8)
